package com.example.panpan.customerManage.service;

import com.example.common.utils.IMap;
import com.example.panpan.customerManage.mapper.TcCustomerMapper;
import com.example.panpan.customerManage.model.TcCustomer;
import com.example.panpan.userManage.mapper.TcUserMapper;
import com.example.panpan.userManage.model.TcUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CustomerService {
    @Autowired
    private TcCustomerMapper tcCustomerMapper;
    @Autowired
    private TcUserMapper tcUserMapper;

    public List<IMap>getCustomerList(IMap param){
        return tcCustomerMapper.getCustomerList(param);
    }

    public int addCustomer(IMap param){
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = tempDate.format(new Date());
        TcCustomer tcCustomer = new TcCustomer();
        tcCustomer.setCustomerId("c"+datetime);
        tcCustomer.setCustomerName(param.getString("customerName"));
        tcCustomer.setType(param.getString("type"));
        tcCustomer.setContact(param.getString("contact"));
        tcCustomer.setSex(param.getString("sex"));
        tcCustomer.setAge(param.getString("age"));
        tcCustomer.setTelephone(param.getString("telephone"));
        tcCustomer.setAddress(param.getString("address"));
        tcCustomer.setCreatUser(param.getString("updateUser"));
        tcCustomer.setCreatDate(new Date());
        tcCustomer.setState("1");
        tcCustomer.setLastUpdateUser(param.getString("updateUser"));
        tcCustomer.setUpdateDate(new Date());
        //检查是否已有系统账户
        TcUser ret = tcUserMapper.selectByPrimaryKey(param.getString("telephone"));
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("telephone"));
        if (ret == null ){
            tcUser.setUserName(param.getString("contact"));
            tcUser.setSex(param.getString("sex"));
            tcUser.setUserPhone(param.getString("telephone"));
            tcUser.setDepartId("00");
            tcUser.setPassword("123456");
            tcUser.setRoleId("04");
            tcUser.setState("1");
            tcUserMapper.insert(tcUser);
        }else if (ret.getState().equals("0")){
            tcUser.setState("1");
            tcUserMapper.updateByPrimaryKeySelective(tcUser);
        }
        try {
            tcCustomerMapper.insert(tcCustomer);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    public IMap getCustomerInfo(String customerId){
        return tcCustomerMapper.getCustomerInfo(customerId);
    }

    public int updCustomer(IMap param){
        TcCustomer tcCustomer = new TcCustomer();
        tcCustomer.setCustomerId(param.getString("customerId"));
        tcCustomer.setCustomerName(param.getString("customerName"));
        tcCustomer.setType(param.getString("type"));
        tcCustomer.setContact(param.getString("contact"));
        tcCustomer.setSex(param.getString("sex"));
        tcCustomer.setAge(param.getString("age"));
        tcCustomer.setTelephone(param.getString("telephone"));
        tcCustomer.setAddress(param.getString("address"));
        tcCustomer.setLastUpdateUser(param.getString("updateUser"));
        tcCustomer.setUpdateDate(new Date());
        //检查是否已有系统账户
        TcUser ret = tcUserMapper.selectByPrimaryKey(param.getString("telephone"));
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("telephone"));
        if (ret == null ){
            tcUser.setUserName(param.getString("contact"));
            tcUser.setSex(param.getString("sex"));
            tcUser.setUserPhone(param.getString("telephone"));
            tcUser.setDepartId("00");
            tcUser.setPassword("123456");
            tcUser.setRoleId("04");
            tcUser.setState("1");
            tcUserMapper.insert(tcUser);
        }else if (ret.getState().equals("0")){
            tcUser.setState("1");
            tcUserMapper.updateByPrimaryKeySelective(tcUser);
        }
        try {
            tcCustomerMapper.updateByPrimaryKeySelective(tcCustomer);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    public int delCustomer(IMap param){
        TcCustomer tcCustomer = new TcCustomer();
        tcCustomer.setCustomerId(param.getString("customerId"));
        tcCustomer.setState("0");
        try {
            tcCustomerMapper.updateByPrimaryKeySelective(tcCustomer);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
